package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.ExportUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.JsonResult;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsDeliveryDetails;
import com.jy.wms.pojo.WmsOrderDistribution;
import com.jy.wms.service.WmsDeliveryDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/WmsDeliveryDetailsController")
@Api(value = "/WmsDeliveryDetailsController", description = "发货明细报表")
public class WmsDeliveryDetailsController extends BaseController<WmsDeliveryDetailsService, WmsDeliveryDetails> {


    @ApiOperation(value = "发货明细", response = JSONObject.class, notes = "发货明细")
    @PostMapping(value = "Shippingdetails")
    public JSONObject Shippingdetails(@RequestBody WmsOrderDistribution map, HttpSession session) throws ServiceException {

        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();

        JSONObject list = baseService.Shippingdetails(map, ckid, hzid);

        return list;
    }

    @ApiOperation(value = "发货明细导出报表", response = JSONObject.class, notes = "发货明细导出报表")
    @PostMapping(value = "/ShippingdetailsExcel")
    public @ResponseBody
    JsonResult ShippingdetailsExcel(@RequestBody WmsDeliveryDetails WmsDeliveryDetails, HttpServletResponse response, HttpServletRequest request, HttpSession session) throws ServiceException, IOException {
        String filename = "发货明细报表";
        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();

        WmsDeliveryDetails.setHzid(hzid);
        WmsDeliveryDetails.setCkid(ckid);
        List<WmsDeliveryDetails> list = null;
        try {
            list = baseService.Shippingdetails(WmsDeliveryDetails);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExportUtils reportExcel = new ExportUtils();
        reportExcel.excelExport(list, filename, WmsDeliveryDetails.class, 1, response, request);
        return renderSuccess("导出成功");
    }
}
